<?php 
if(!defined('BASEPATH'))
		exit('No direct script access allowed');
	class Offer extends CI_Controller {
		function __construct(){
			session_start();
			parent::__construct();
		}
		
		function index($query_id=0, $order_by='created_date', $sort_order='asc', $current_page=0){
			$search_by='';
			$search_by_value='';
				if($query_id == '0'){		
					//echo '<pre>';print_r($_SESSION);exit;
					unset($_SESSION['search']);
				}else{
					if(isset($_SESSION['search'])){
					$array = $_SESSION['search'];
					//echo '<pre>';print_r($_SESSION);exit;
					$search_by = $array['search_by'];
					$search_by_value = $array['search'];
					//echo '<pre>';print_r($search_by_value);exit;
					
					}
				}
				
			$results_per_page = 3;
		
		
			$url = CURL_URL;
			$data = array('www-command'=>'events-all_event_offer', 'current_page'=>$current_page, 'results_per_page'=>$results_per_page, 'order_by'=>$order_by, 'sort_order'=>$sort_order, 'search_by'=>$search_by, 'search_by_value'=>$search_by_value);
			
			$string = http_build_query($data);
			//echo '<pre>';print_r($data);exit;
			$ch = curl_init($url);
			curl_setopt($ch,CURLOPT_POST,true);
			curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
			$response= json_decode(curl_exec($ch));
			//echo '<pre>';print_r($response);exit;
			curl_close($ch);
			if($response->message !== 'No offers'){
			$this->load->library('pagination');			
			$config['base_url'] = base_url().'admin/offer/index/'.$query_id.'/'.$order_by.'/'.$sort_order;
			$config['per_page'] = $results_per_page;
			$config['total_rows']  = $response->total;
			$config['uri_segment'] = 7;
			$config['num_links'] = 1;
			$config['prev_link'] = 'Previous';
			$config['next_link'] = 'Next';
			$config['first_link'] = 'First';
			$config['last_link'] = 'Last';
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['events_all_event_offer'] = $response;
			$data['offer'] = '';
			}else{
				$data['offer'] = 1;
				$data['events_all_event_offer'] = '';
			}
			//echo '<pre>';print_r($data);exit;
			$data['sort_order'] = $sort_order;
			$data['order_by'] = $order_by;
			$data['query_id'] = $query_id;
			$data['search_by'] = $search_by;
			$data['search_by_value'] = $search_by_value;
			$data['offer_fields'] = array('offer_code'=>'Offer Code', 'description'=>'Description', 'start_date'=>'Start Date', 'end_date'=>'End Date', 'disc_amount'=>'Discount Amount/Percentage', 'status'=>'Status');
			$data['status'] = array(1=>'Active', 0=>'In-active');
			$this->load->view('admin/offer', $data);
	}
	
	function search_by(){
		if($this->input->post('search') == ''){
				$search = $this->input->post('search_date');
			}else{
				$search = $this->input->post('search');	
			}
			
			$query_array = array(
				'search_by'=>$this->input->post('search_by'),
				'search'=>$search
			);
			$query_id=rand();
			$_SESSION['search']=$query_array;
			//echo '<pre>';print_r($_SESSION['search']);exit;
			redirect('admin/offer/index/'.$query_id);
			
		}
	
	function create_offer_page(){
		$this->load->view('admin/create_offer_page');
	}
	
	function create_offer(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('offer', 'Offer', 'required');
				$this->form_validation->set_rules('start', 'Start Date', 'required');
				$this->form_validation->set_rules('end', 'End Date', 'required');
				$this->form_validation->set_rules('description', 'Description', 'required|max_length[200]');
				
				if($this->form_validation->run() == FALSE){
						$this->load->view('admin/create_offer_page');
				}else{
					$url = CURL_URL;
					if($this->input->post('ticket') == 'on'){
						$free_ticket = $this->input->post('free_ticket');
						$min_ticket = $this->input->post('min_ticket');
						$disc_type = '';
						$min_amount = '';
						$disc_amount = '';
					}else{
						$disc_type = $this->input->post('disc_type');
						$min_amount = $this->input->post('min_amount');
						$disc_amount = $this->input->post('disc_amount');
						$free_ticket = '';
						$min_ticket = '';
					}
					$data = array('www-command'=>'events-create_event_offer', 
					'offer_code' => $this->input->post('offer'),
					'start_date' => $this->input->post('start'),
					'end_date' => $this->input->post('end'),
					'description' => $this->input->post('description'),
					'minimum_tickets' => $min_ticket,
					'free_tickets' => $free_ticket,
					'discount_type' => $disc_type,
					'minimum_amount' => $min_amount,
					'disc_amount' => $disc_amount
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/offer/index/');
				}
			}
			
			function edit_offer(){
				$this->load->library('form_validation');
				$this->form_validation->set_error_delimiters('<span style="color:red">','</span>');
				$this->form_validation->set_rules('offer', 'Offer', 'required');
				$this->form_validation->set_rules('start', 'Start Date', 'required');
				$this->form_validation->set_rules('end', 'End Date', 'required');
				$this->form_validation->set_rules('description', 'Description', 'required|max_length[200]');
				
				if($this->form_validation->run() == FALSE){
							if($this->uri->segment(4) > 0){
					$_SESSION['offer_id'] = $this->uri->segment(4);
				}
					 	$url = CURL_URL;
					$data = array('www-command'=>'events-get_event_offer', 
					'offer_id' => $_SESSION['offer_id']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					$data['get_event_offer'] = $response;
					//echo '<pre>';print_r($data['get_event_offer']);exit;
					curl_close($ch); 
					$this->load->view('admin/edit_offer_page', $data);
					
				}else{
					$url = CURL_URL;
					if($this->input->post('status') == 'on'){
						$status = 1;
					}else{
						$status = 0;
					}
					if($this->input->post('ticket') == 'on'){
						$free_ticket = $this->input->post('free_ticket');
						$min_ticket = $this->input->post('min_ticket');
						$disc_type = '';
						$min_amount = '';
						$disc_amount = '';
					}else{
						$disc_type = $this->input->post('disc_type');
						$min_amount = $this->input->post('min_amount');
						$disc_amount = $this->input->post('disc_amount');
						$free_ticket = '';
						$min_ticket = '';
					}
					$data = array('www-command'=>'events-edit_event_offer', 
					'offer_code' => $this->input->post('offer'),
					'start_date' => $this->input->post('start'),
					'end_date' => $this->input->post('end'),
					'description' => $this->input->post('description'),
					'minimum_tickets' => $min_ticket,
					'free_tickets' => $free_ticket,
					'discount_type' => $disc_type,
					'minimum_amount' => $min_amount,
					'disc_amount' => $disc_amount,
					'status' => $status,
					'offer_id' => $_SESSION['offer_id']
					);
					//echo '<pre>';print_r($data);exit;
					$string = http_build_query($data);
					$ch = curl_init($url);
					curl_setopt($ch,CURLOPT_POST,true);
					curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
					curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
					$response= json_decode(curl_exec($ch));
					//echo '<pre>';print_r($response);exit;
					curl_close($ch);
					redirect('admin/offer/index/');
				}
			}
			
			function delete_offer(){
				$url = CURL_URL;
				if($this->uri->segment(5) == 0){
					$status = 1;
				}else{
					$status = 0;
				}
				$data = array('www-command'=>'events-delete_event_offer','status'=>$status, 'offer_id'=>$this->uri->segment(4));
				$string = http_build_query($data);
				$ch = curl_init($url);
				curl_setopt($ch,CURLOPT_POST,true);
				curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
				curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);			
				$response = json_decode(curl_exec($ch));
				
				redirect('admin/offer/index/');
			}
}